﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackupAssurance.Library.MEF;
using System.ComponentModel.Composition;
using BackupAssurance.Library.Interfaces;
using BackupAssurance.Library.ModelsDB;
using BackupAssurance.Library.XML;
using BackupAssurance.Library.Constants;

namespace BackupAssurance.impMountDrive
{


    [Export(typeof(IExecute)), ExportMetadata("PlugInType", "MountDrive"), ExportMetadata("Version", 1)]
    public class piMapDrive : IExecute
    {
        [Import]
        public IfLogger objLog { get; set; }


        string _name = null;

        public piMapDrive()
        {

        }

        //map
        //<resource key="Action">Map</resource>
        //<resource key="Force">True</resource>
        //<resource key="Persistent">True</resource>
        //<resource key="LocalDrive">T</resource>
        //<resource key="PromptForCredentials">False</resource>
        //<resource key="ShareName"></resource>
        //<resource key="SaveCredentials">False</resource>
        //<resource key="Login">False</resource>
        //<resource key="Password">False</resource>

        //unmap
        //<resource key="Action">UnMap</resource>
        //<resource key="Force">True</resource>
        //<resource key="Persistent">True</resource>
        //<resource key="LocalDrive">T</resource>

        public string Execute(pcJobAction jobaction, pcPlugInValue plugininfo)
        {
            string strResult = "";
            string Action;
            Boolean Force;
            Boolean Persistent;
            string LocalDrive;
            Boolean PromptForCredentials;
            string ShareName;
            Boolean SaveCredentials;
            string Login;
            string Password;
            string output;

            _name = " START, Computer:" + System.Environment.MachineName + " MountDrive v1,  Job ID:" + jobaction.Id.ToString() + " Plugin ID:" + plugininfo.Id.ToString() + Environment.NewLine + "       [" + plugininfo.PlugInText + "]";

            try
            {
                Dictionary<string, string> objDic;
                objLog.WriteLine("Plugin Start: " + _name);
                //objLog.WriteLine(plugininfo.PlugInText);
                objDic = fXMLParser.ConvertXMLtoDictionary(plugininfo.PlugInText);
                Action = objDic[Constants.PLG_ACTION];

                Action = Action.ToUpper();

                //throw new Exception("error mapping drive");

                switch (Action)
                {
                    case Constants.PLG_ACTION_MAP:
                        Force = Boolean.Parse(objDic[Constants.PLG_FORCE]);
                        Persistent = Boolean.Parse(objDic[Constants.PLG_PERSISTENT]);
                        PromptForCredentials = Boolean.Parse(objDic[Constants.PLG_PROMPTFORCREDENTIALS]);
                        SaveCredentials = Boolean.Parse(objDic[Constants.PLG_SAVECREDENTIALS]);
                        LocalDrive = objDic[Constants.PLG_LOCALDRIVE];
                        ShareName = objDic[Constants.PLG_SHARENAME];
                        Login = objDic[Constants.PLG_LOGIN];
                        Password = objDic[Constants.PLG_PASSWORD];
                        objLog.WriteLine("Begin Drive Map -->");
                        strResult = mapdrive(Force, Persistent, LocalDrive, PromptForCredentials, ShareName, SaveCredentials, Login, Password);
                        objLog.WriteLine("End Drive Map --< " + strResult);
                        break;

                    case Constants.PLG_ACTION_UNMAP:
                        Force = Boolean.Parse(objDic[Constants.PLG_FORCE]);
                        Persistent = Boolean.Parse(objDic[Constants.PLG_PERSISTENT]);
                        LocalDrive = objDic[Constants.PLG_LOCALDRIVE];
                        strResult = unmapdrive(Force, Persistent, LocalDrive);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                output = _name + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace;
                objLog.WriteError(0, output);
                throw new Exception(output);
            }

            return strResult;

        }

        private string mapdrive(Boolean Force, Boolean Persistent, string LocalDrive, Boolean PromptForCredentials, string ShareName, Boolean SaveCredentials, string LoginName, string Password)
        {

            string Results = "";

            try
            {
                NetMap _oNetDrive = new NetMap();

                _oNetDrive.Force = Force;
                _oNetDrive.Persistent = Persistent;
                _oNetDrive.LocalDrive = LocalDrive;
                _oNetDrive.PromptForCredentials = PromptForCredentials;
                _oNetDrive.ShareName = ShareName;
                _oNetDrive.SaveCredentials = SaveCredentials;

                //output = String.Format("{0},{1}", LoginName, Password);
                //objLog.WriteLine(output);

                //match call to options provided
                if (LoginName == "" && Password == "")
                {
                    _oNetDrive.MapDrive();
                }
                else if (LoginName == "")
                {
                    _oNetDrive.MapDrive(Password);
                }
                else
                {
                    _oNetDrive.MapDrive(LoginName, Password);
                }
                Results = "Result: Drive " + LocalDrive + " mapped successfully. " + _name;
                objLog.WriteInfo(0, Results);
            }
            catch (Exception err)
            {
                Results = "Result: Failed to map drive " + LocalDrive + ". " + err.Message + " " + _name;
                objLog.WriteError(0, Results);
                throw new Exception(Results + Environment.NewLine + err.Message + Environment.NewLine + err.StackTrace);
            }
            return Results;

        }


        private string unmapdrive(Boolean Force, Boolean Persistent, string LocalDrive)
        {
            string Results;

            try
            {
                NetMap _oNetDrive = new NetMap();
                //unmap the drive
                _oNetDrive.Force = Force;
                _oNetDrive.LocalDrive = LocalDrive;
                _oNetDrive.Persistent = Persistent;
                _oNetDrive.UnMapDrive();

                Results = "Result: Drive " + LocalDrive + " unmapped successfully. " + _name;
                objLog.WriteInfo(0, Results);

            }
            catch (Exception err)
            {
                Results = "Result: Failed to unmap drive " + LocalDrive + ". " + err.Message + " " + _name;
                objLog.WriteInfo(0, Results);
                throw new Exception(Results + Environment.NewLine + err.Message + Environment.NewLine + err.StackTrace);
            }

            return Results;
        }

        public void Dispose()
        {

        }

    }
}
